﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_DTNhapHang : BasePage
    {
        #region Fields
        public int tongSoPhieu = 0;
        public double tongTien = 0;
        public string thanhtienText = string.Empty;
        public string thanhtienNumber = string.Empty;
        #endregion

        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {
                InitData();
                LoadData();
            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }

        #endregion

        #region Methods

        public void InitTextControl()
        {
            btnSearch.Text = "Thống kê";
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
        }
        private void InitData()
        {
            vsumSumary.ValidationGroup = cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = btnSearch.ValidationGroup = "phieuhang";
            cusvNgayLapPhieuFrom.ErrorMessage = cusvNgayLapPhieuTo.ErrorMessage = string.Format(Resources.Resource.MESSAGE_VALIDATE_INPUT, CommonFunction.GetTextLabel(lblNgayNhap));
            cusvNgayLapPhieuFrom.ValidationGroup = cusvNgayLapPhieuTo.ValidationGroup = "phieuhang";
            CommonFunction.LoadSubCate(drlKho, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
            LoadDVT();

        }
        public void LoadDVT()
        {
            List<CurencyResult> lstCate = CurencyUtil.GetCurencyDTNhapHang();
            ListItem nullItem = new ListItem(BaseFunctions.COMBOBOX_NULL_VALUE, string.Empty);
            drlCurency.Items.Add(nullItem);
            if (lstCate == null)
                return;
            foreach (CurencyResult objCate in lstCate)
            {
               
                drlCurency.Items.Add(new ListItem(objCate.Name, objCate.Code.ToString()));
            }
        }
        public void LoadData()
        {
            string result = string.Empty;
            if(drlCurency.SelectedValue!=string.Empty)
                result+=LoadDataByCurrency(drlCurency.SelectedValue,drlCurency.SelectedItem.Text);
            else
            {
                foreach(ListItem objItem in drlCurency.Items)
                {
                    if (objItem.Value != string.Empty)
                     result+=LoadDataByCurrency(objItem.Value,objItem.Text);
                }
            }
            if (result == string.Empty)
            {
                result = "<table><tr><td>Không có phiếu nhập nào thỏa điều kiện nhập</td></tr></table>";
            }
            else
            {
                result ="<table>" + result +  "</table>";
            }
            
            tdData.InnerHtml = result;
        }
        public string LoadDataByCurrency(string curencyCode,string name)
        {

            string condition = " where [dbo].[fcCurrencyInPhieuNhapHang]('" + curencyCode + "',ph.MaPhieuNhapHang)=1";
            if (drlKho.SelectedValue != string.Empty)
                condition += " and ph.maKho=" + drlKho.SelectedValue;
            if (txtMaMatHang.Text != string.Empty)
                condition += " and [dbo].[fcMaMatHangInPhieuNhapHang]('" + txtMaMatHang.Text.Trim() + "',ph.MaPhieuNhapHang)=1";
            if (txtMaPhuTung.Text != string.Empty)
                condition += " and [dbo].[fcMaPhuTungInPhieuNhapHang]('" + txtMaPhuTung.Text.Trim() + "',ph.MaPhieuNhapHang)=1";
            if (txtNgayLapPhieuFrom.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuFrom.Text);
                condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.NgayNhap)>=0";
            }
            if (txtNgayLapPhieuTo.Text != string.Empty)
            {
                DateTime dt = ConvertValueObject.ParseDateTime(txtNgayLapPhieuTo.Text);
                condition += " and DATEDIFF(day,'" + dt.Year + "-" + dt.Month + "-" + dt.Day + "',ph.NgayNhap)<=0";
            }
            MatHangTonKhoUtil.ThongKeDoanhThuNhapHang(condition, ref tongSoPhieu, ref tongTien);
            thanhtienNumber = ConvertValueObject.ParseString(tongTien);
            thanhtienText = CommonFunction.ReadNumber(thanhtienNumber) + " " + name;
            //CommonFunction.ReadNumber(ConvertValueObject.ParseString(total)) + " " + objCurency.Name;
            if (tongSoPhieu < 1)
                return string.Empty;
           string result=string.Empty;
           result += @"<tr>
                            <td>
                                <b>+" + name + @"</b> 
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Tổng số phiếu nhập:" + tongSoPhieu + @"
                                &nbsp;&nbsp;&nbsp;&nbsp;>><a href='DMPhieuNhapHang.aspx?kho=" + drlKho.SelectedValue + @"&cx=&mamh=" + txtMaMatHang.Text + "&mapt=" + txtMaPhuTung.Text + "&sdate=" + txtNgayLapPhieuFrom.Text + "&edate=" + txtNgayLapPhieuTo.Text + "&c=" + curencyCode +@"'>Xem
                                    DS phiếu nhập</a>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Tổng thành tiền:" + thanhtienNumber + "&nbsp;&nbsp;" + name + @"<span class='textCurency'>(" + thanhtienText + @")</span>
                            </td>
                        </tr>";
           return result;
        }
       

        private string CreateRowData(string curencyCode)
        {
            string result = string.Empty;
            return result;
        }

        #endregion

        #region Events,Handler


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }


        #endregion
    }
}
